// pages/user/login/index.js
const config = require("../../../utils/config.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  reg() {
    wx.navigateTo({
      url: '../add/index',
    })
  },
  login() {
    let _this = this
    wx.login({
      success(res) {
        console.log(res)
        wx.request({
          url: config.HOST+'/user/login',
          data: {
            code:res.code
          },
          success(loginRes) {
            console.log(loginRes)
            if(loginRes.data.status==200) {
              //保存token
              wx.setStorageSync("token", loginRes.data.data)
              
              wx.showToast({
                title: '登录成功',
                duration:3000,
                success() {
                  //获取用户信息
                  _this._getInfo()
                  wx.navigateBack({})
                }
              })
            }else{
              wx.showToast({
                title: loginRes.data.message,
                icon: 'none'
              })
            }
          }
        })
      }
    })
  },
  _getInfo() {
    let token = wx.getStorageSync("token")
    if (token == null || token == '' || token == undefined)
      return;
    wx.request({
      url: config.HOST + '/user/getUserInfo',
      data: {
        token
      },
      success: (res) => {
        console.log(res.data)
        if (res.data.status == 200) {
          wx.setStorageSync("userInfo", JSON.stringify(res.data.data))
        }
      }
    })
  },
})